/**
 * 
 */
package com.android.lovepick.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author Liutong
 */
public class DatabaseHelpler extends SQLiteOpenHelper
{
	private static final String DATABASE_TAG = "DatabaseHelper";
	private static final String NAME = "order.db";
	private final String CREATE_ORDER_SQL = "create table order("
			+ "orderID interger primary key autoincrement, "
			+ "nickName varchar(32) not null, " + "goTime datetime not null, "
			+ "src varchar(32) not null, " + "slat double not null, "
			+ "slng double not null, " + "dest varchar(32) not null, "
			+ "dlat double not null, " + "dlng double not null, "
			+ "money interger not null, " + "state varchar(25))";
	private final String DROP_ORDER_SQL = "drop table order";

	/**
	 * @param context
	 * @param name
	 * @param factory
	 * @param version
	 */
	public DatabaseHelpler(Context context, String name, CursorFactory factory,
			int version)
	{
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	public DatabaseHelpler(Context context)
	{
		super(context, NAME, null, 1);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		// TODO Auto-generated method stub
		Log.i(DATABASE_TAG, "---create database---");
		db.execSQL(CREATE_ORDER_SQL);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// TODO Auto-generated method stub
		Log.i(DATABASE_TAG, "---upgrade database---");
		db.execSQL(DROP_ORDER_SQL);
		onCreate(db);
	}

	/**
	 * upgrade according to the version number
	 * 
	 * @param db the database to upgrade
	 * @param newVersion the new version
	 */
	public void checkVersionCreate(SQLiteDatabase db, int newVersion)
	{
		int version = db.getVersion();
		if (version != newVersion)
		{
			db.beginTransaction();
			try
			{
				if (version == 0)
				{
					onCreate(db);
				}
				else
				{
					onUpgrade(db, version, newVersion);
				}
				// set new version
				db.setVersion(newVersion);
				db.setTransactionSuccessful();
			}
			catch (Exception e)
			{
				// TODO: handle exception
				e.printStackTrace();
			}
			finally
			{
				db.endTransaction();
			}
		}
	}

	public SQLiteDatabase getDatabase(SQLiteDatabase db, Context context,
			String name, CursorFactory factory)
	{
		if ((db != null) && (db.isOpen()) && (!db.isReadOnly()))
		{
			return db;
		}
		if (name == null)
		{
			db = SQLiteDatabase.create(null);
		}
		else
		{
			db = context.openOrCreateDatabase(name, 0, factory);
		}
		return db;
	}

}
